home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / elf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  42.2 KB  |  1,408 lines

  1. /* ELF support for BFD.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4.    Written by Fred Fish @ Cygnus Support, from information published
  5.    in "UNIX System V Release 4, Programmers Guide: ANSI C and
  6.    Programming Support Tools".
  7.  
  8. This file is part of BFD, the Binary File Descriptor library.
  9.  
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24.  
  25.     /****************************************
  26.  
  27.               WARNING
  28.  
  29.     This is only a partial ELF implementation,
  30.     incorporating only those parts that are
  31.     required to get gdb up and running.  It is
  32.     expected that it will be expanded to a full
  33.     ELF implementation at some future date.
  34.  
  35.     Unimplemented stubs call abort() to ensure
  36.     that they get proper attention if they are
  37.     ever called.  The stubs are here since
  38.     this version was hacked from the COFF
  39.     version, and thus they will probably
  40.     go away or get expanded appropriately in a
  41.     future version.
  42.  
  43.     fnf@cygnus.com
  44.  
  45.     *****************************************/
  46.  
  47.  
  48. /* Problems and other issues to resolve.
  49.  
  50.    (1)    BFD expects there to be some fixed number of "sections" in
  51.         the object file.  I.E. there is a "section_count" variable in the
  52.     bfd structure which contains the number of sections.  However, ELF
  53.     supports multiple "views" of a file.  In particular, with current
  54.     implementations, executable files typically have two tables, a
  55.     program header table and a section header table, both of which
  56.     partition the executable.
  57.  
  58.     In ELF-speak, the "linking view" of the file uses the section header
  59.     table to access "sections" within the file, and the "execution view"
  60.     uses the program header table to access "segments" within the file.
  61.     "Segments" typically may contain all the data from one or more
  62.     "sections".
  63.  
  64.     Note that the section header table is optional in ELF executables,
  65.     but it is this information that is most useful to gdb.  If the
  66.     section header table is missing, then gdb should probably try
  67.     to make do with the program header table.  (FIXME)
  68.  
  69. */
  70.  
  71. #include "bfd.h"
  72. #include "sysdep.h"
  73. #include "libbfd.h"
  74. #include "obstack.h"
  75. #include "elf/common.h"
  76. #include "elf/internal.h"
  77. #include "elf/external.h"
  78.  
  79. #ifdef HAVE_PROCFS    /* Some core file support requires host /proc files */
  80. #include <sys/procfs.h>
  81. #else
  82. #define bfd_prstatus(abfd, descdata, descsz, filepos)    /* Define away */
  83. #define bfd_fpregset(abfd, descdata, descsz, filepos)    /* Define away */
  84. #define bfd_prpsinfo(abfd, descdata, descsz, filepos)    /* Define away */
  85. #endif
  86.  
  87. /* Forward data declarations */
  88.  
  89. extern bfd_target elf_little_vec, elf_big_vec;
  90.  
  91. /* Currently the elf_symbol_type struct just contains the generic bfd
  92.    symbol structure. */
  93.  
  94. typedef struct
  95. {
  96.   asymbol symbol;
  97. } elf_symbol_type;
  98.  
  99. /* Some private data is stashed away for future use using the tdata pointer
  100.    in the bfd structure.  This information is different for ELF core files
  101.    and other ELF files. */
  102.  
  103. typedef struct elf_core_tdata_struct
  104. {
  105.   void *prstatus;        /* The raw /proc prstatus structure */
  106.   void *prpsinfo;        /* The raw /proc prpsinfo structure */
  107. } elf_core_tdata;
  108.  
  109. #define core_prpsinfo(bfd) (((bfd)->tdata.elf_core_data) -> prpsinfo)
  110. #define core_prstatus(bfd) (((bfd)->tdata.elf_core_data) -> prstatus)
  111.  
  112.  
  113. typedef struct elf_obj_tdata_struct
  114. {
  115.   file_ptr symtab_filepos;    /* Offset to start of ELF symtab section */
  116.   long symtab_filesz;        /* Size of ELF symtab section */
  117.   file_ptr strtab_filepos;    /* Offset to start of ELF string tbl section */
  118.   long strtab_filesz;        /* Size of ELF string tbl section */
  119. } elf_obj_tdata;
  120.  
  121. #define elf_tdata(bfd)        ((bfd) -> tdata.elf_obj_data)
  122. #define elf_symtab_filepos(bfd)    (elf_tdata(bfd) -> symtab_filepos)
  123. #define elf_symtab_filesz(bfd)    (elf_tdata(bfd) -> symtab_filesz)
  124. #define elf_strtab_filepos(bfd)    (elf_tdata(bfd) -> strtab_filepos)
  125. #define elf_strtab_filesz(bfd)    (elf_tdata(bfd) -> strtab_filesz)
  126.  
  127. /* Translate an ELF symbol in external format into an ELF symbol in internal
  128.    format. */
  129.  
  130. static void
  131. DEFUN(elf_swap_symbol_in,(abfd, src, dst),
  132.       bfd               *abfd AND
  133.       Elf_External_Sym *src AND
  134.       Elf_Internal_Sym *dst)
  135. {
  136.   dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name);
  137.   dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value);
  138.   dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size);
  139.   dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info);
  140.   dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other);
  141.   dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx);
  142. }
  143.  
  144.  
  145. /* Translate an ELF file header in external format into an ELF file header in
  146.    internal format. */
  147.  
  148. static void
  149. DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
  150.       bfd               *abfd AND
  151.       Elf_External_Ehdr *src AND
  152.       Elf_Internal_Ehdr *dst)
  153. {
  154.   memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
  155.   dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
  156.   dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
  157.   dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
  158.   dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
  159.   dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
  160.   dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
  161.   dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
  162.   dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
  163.   dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
  164.   dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
  165.   dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
  166.   dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
  167.   dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
  168. }
  169.  
  170.  
  171. /* Translate an ELF section header table entry in external format into an
  172.    ELF section header table entry in internal format. */
  173.  
  174. static void
  175. DEFUN(elf_swap_shdr_in,(abfd, src, dst),
  176.       bfd               *abfd AND
  177.       Elf_External_Shdr *src AND
  178.       Elf_Internal_Shdr *dst)
  179. {
  180.   dst -> sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_name);
  181.   dst -> sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_type);
  182.   dst -> sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_flags);
  183.   dst -> sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addr);
  184.   dst -> sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_offset);
  185.   dst -> sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_size);
  186.   dst -> sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_link);
  187.   dst -> sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_info);
  188.   dst -> sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addralign);
  189.   dst -> sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_entsize);
  190. }
  191.  
  192.  
  193. /* Translate an ELF program header table entry in external format into an
  194.    ELF program header table entry in internal format. */
  195.  
  196. static void
  197. DEFUN(elf_swap_phdr_in,(abfd, src, dst),
  198.       bfd               *abfd AND
  199.       Elf_External_Phdr *src AND
  200.       Elf_Internal_Phdr *dst)
  201. {
  202.   dst -> p_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_type);
  203.   dst -> p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_offset);
  204.   dst -> p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_vaddr);
  205.   dst -> p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_paddr);
  206.   dst -> p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_filesz);
  207.   dst -> p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_memsz);
  208.   dst -> p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_flags);
  209.   dst -> p_align = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_align);
  210. }
  211.  
  212.  
  213. /* Create a new bfd section from an ELF section header. */
  214.  
  215. static boolean
  216. DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab),
  217.       bfd               *abfd AND
  218.       Elf_Internal_Shdr *hdr AND
  219.       char        *shstrtab)
  220. {
  221.   asection *newsect;
  222.   char *name;
  223.  
  224.   name = hdr -> sh_name ? shstrtab + hdr -> sh_name : "unnamed";
  225.   newsect = bfd_make_section (abfd, name);
  226.   if (!newsect) return false;
  227.   newsect -> vma = hdr -> sh_addr;
  228.   newsect -> _raw_size = hdr -> sh_size;
  229.   if (!(hdr -> sh_type == SHT_NOBITS))
  230.     {
  231.       newsect -> filepos = hdr -> sh_offset;
  232.       newsect -> flags |= SEC_HAS_CONTENTS;
  233.     }
  234.   if (hdr -> sh_flags & SHF_ALLOC)
  235.     {
  236.       newsect -> flags |= SEC_ALLOC;
  237.       if (hdr -> sh_type != SHT_NOBITS)
  238.     {
  239.       newsect -> flags |= SEC_LOAD;
  240.     }
  241.     }
  242.   if (!(hdr -> sh_flags & SHF_WRITE))
  243.     {
  244.       newsect -> flags |= SEC_READONLY;
  245.     }
  246.   if (hdr -> sh_flags & SHF_EXECINSTR)
  247.     {
  248.       newsect -> flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
  249.     }
  250.   else
  251.     {
  252.       newsect -> flags |= SEC_DATA;
  253.     }
  254.   if (hdr -> sh_type == SHT_SYMTAB)
  255.     {
  256.       abfd -> flags |= HAS_SYMS;
  257.     }
  258.  
  259.   return (true);
  260. }
  261.  
  262. /* Create a new bfd section from an ELF program header.
  263.  
  264.    Since program segments have no names, we generate a synthetic name
  265.    of the form segment<NUM>, where NUM is generally the index in the
  266.    program header table.  For segments that are split (see below) we
  267.    generate the names segment<NUM>a and segment<NUM>b.
  268.  
  269.    Note that some program segments may have a file size that is different than
  270.    (less than) the memory size.  All this means is that at execution the
  271.    system must allocate the amount of memory specified by the memory size,
  272.    but only initialize it with the first "file size" bytes read from the
  273.    file.  This would occur for example, with program segments consisting
  274.    of combined data+bss.
  275.  
  276.    To handle the above situation, this routine generates TWO bfd sections
  277.    for the single program segment.  The first has the length specified by
  278.    the file size of the segment, and the second has the length specified
  279.    by the difference between the two sizes.  In effect, the segment is split
  280.    into it's initialized and uninitialized parts.
  281.  
  282.  */
  283.  
  284. static boolean
  285. DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
  286.       bfd               *abfd AND
  287.       Elf_Internal_Phdr *hdr AND
  288.       int         index)
  289. {
  290.   asection *newsect;
  291.   char *name;
  292.   char namebuf[64];
  293.   int split;
  294.  
  295.   split = ((hdr -> p_memsz > 0) &&
  296.        (hdr -> p_filesz > 0) &&
  297.        (hdr -> p_memsz > hdr -> p_filesz));
  298.   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
  299.   name = bfd_alloc (abfd, strlen (namebuf) + 1);
  300.   (void) strcpy (name, namebuf);
  301.   newsect = bfd_make_section (abfd, name);
  302.   newsect -> vma = hdr -> p_vaddr;
  303.   newsect -> _raw_size = hdr -> p_filesz;
  304.   newsect -> filepos = hdr -> p_offset;
  305.   newsect -> flags |= SEC_HAS_CONTENTS;
  306.   if (hdr -> p_type == PT_LOAD)
  307.     {
  308.       newsect -> flags |= SEC_ALLOC;
  309.       newsect -> flags |= SEC_LOAD;
  310.       if (hdr -> p_flags & PF_X)
  311.     {
  312.       /* FIXME: all we known is that it has execute PERMISSION,
  313.          may be data. */
  314.       newsect -> flags |= SEC_CODE;
  315.     }
  316.     }
  317.   if (!(hdr -> p_flags & PF_W))
  318.     {
  319.       newsect -> flags |= SEC_READONLY;
  320.     }
  321.  
  322.   if (split)
  323.     {
  324.       sprintf (namebuf, "segment%db", index);
  325.       name = bfd_alloc (abfd, strlen (namebuf) + 1);
  326.       (void) strcpy (name, namebuf);
  327.       newsect = bfd_make_section (abfd, name);
  328.       newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
  329.       newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
  330.       if (hdr -> p_type == PT_LOAD)
  331.     {
  332.       newsect -> flags |= SEC_ALLOC;
  333.       if (hdr -> p_flags & PF_X)
  334.         {
  335.           newsect -> flags |= SEC_CODE;
  336.         }
  337.     }
  338.       if (!(hdr -> p_flags & PF_W))
  339.     {
  340.       newsect -> flags |= SEC_READONLY;
  341.     }
  342.     }
  343.  
  344.   return (true);
  345. }
  346.  
  347. #ifdef HAVE_PROCFS
  348.  
  349. static void
  350. DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
  351.       bfd    *abfd AND
  352.       char    *descdata AND
  353.       int     descsz AND
  354.       long     filepos)
  355. {
  356.   asection *newsect;
  357.   prstatus_t *status = (prstatus_t *)0;
  358.  
  359.   if (descsz == sizeof (prstatus_t))
  360.     {
  361.       newsect = bfd_make_section (abfd, ".reg");
  362.       newsect -> _raw_size = sizeof (status->pr_reg);
  363.       newsect -> filepos = filepos + (long) &status->pr_reg;
  364.       newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  365.       newsect -> alignment_power = 2;
  366.       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  367.     {
  368.       memcpy (core_prstatus (abfd), descdata, descsz);
  369.     }
  370.     }
  371. }
  372.  
  373. /* Stash a copy of the prpsinfo structure away for future use. */
  374.  
  375. static void
  376. DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos),
  377.       bfd    *abfd AND
  378.       char    *descdata AND
  379.       int     descsz AND
  380.       long     filepos)
  381. {
  382.   asection *newsect;
  383.  
  384.   if (descsz == sizeof (prpsinfo_t))
  385.     {
  386.       if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  387.     {
  388.       bcopy (descdata, core_prpsinfo (abfd), descsz);
  389.     }
  390.     }
  391. }
  392.  
  393. static void
  394. DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos),
  395.       bfd    *abfd AND
  396.       char    *descdata AND
  397.       int     descsz AND
  398.       long     filepos)
  399. {
  400.   asection *newsect;
  401.  
  402.   newsect = bfd_make_section (abfd, ".reg2");
  403.   newsect -> _raw_size = descsz;
  404.   newsect -> filepos = filepos;
  405.   newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  406.   newsect -> alignment_power = 2;
  407. }
  408.  
  409. #endif    /* HAVE_PROCFS */
  410.  
  411. /* Return a pointer to the args (including the command name) that were
  412.    seen by the program that generated the core dump.  Note that for
  413.    some reason, a spurious space is tacked onto the end of the args
  414.    in some (at least one anyway) implementations, so strip it off if
  415.    it exists. */
  416.  
  417. char *
  418. DEFUN(elf_core_file_failing_command, (abfd),
  419.      bfd *abfd)
  420. {
  421. #ifdef HAVE_PROCFS
  422.   if (core_prpsinfo (abfd))
  423.     {
  424.       prpsinfo_t *p = core_prpsinfo (abfd);
  425.       char *scan = p -> pr_psargs;
  426.       while (*scan++) {;}
  427.       scan -= 2;
  428.       if ((scan > p -> pr_psargs) && (*scan == ' '))
  429.     {
  430.       *scan = '\000';
  431.     }
  432.       return (p -> pr_psargs);
  433.     }
  434. #endif
  435.   return (NULL);
  436. }
  437.  
  438. /* Return the number of the signal that caused the core dump.  Presumably,
  439.    since we have a core file, we got a signal of some kind, so don't bother
  440.    checking the other process status fields, just return the signal number.
  441.    */
  442.  
  443. static int
  444. DEFUN(elf_core_file_failing_signal, (abfd),
  445.       bfd *abfd)
  446. {
  447. #ifdef HAVE_PROCFS
  448.   if (core_prstatus (abfd))
  449.     {
  450.       return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
  451.     }
  452. #endif
  453.   return (-1);
  454. }
  455.  
  456. /* Check to see if the core file could reasonably be expected to have
  457.    come for the current executable file.  Note that by default we return
  458.    true unless we find something that indicates that there might be a
  459.    problem.
  460.    */
  461.  
  462. static boolean
  463. DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
  464.       bfd *core_bfd AND
  465.       bfd *exec_bfd)
  466. {
  467. #ifdef HAVE_PROCFS
  468.   char *corename;
  469.   char *execname;
  470. #endif
  471.  
  472.   /* First, xvecs must match since both are ELF files for the same target. */
  473.  
  474.   if (core_bfd->xvec != exec_bfd->xvec)
  475.     {
  476.       bfd_error = system_call_error;
  477.       return (false);
  478.     }
  479.  
  480. #ifdef HAVE_PROCFS
  481.  
  482.   /* If no prpsinfo, just return true.  Otherwise, grab the last component
  483.      of the exec'd pathname from the prpsinfo. */
  484.  
  485.   if (core_prpsinfo (core_bfd))
  486.     {
  487.       corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname);
  488.     }  
  489.   else
  490.     {
  491.       return (true);
  492.     }
  493.  
  494.   /* Find the last component of the executable pathname. */
  495.  
  496.   if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL)
  497.     {
  498.       execname++;
  499.     }
  500.   else
  501.     {
  502.       execname = (char *) exec_bfd -> filename;
  503.     }
  504.  
  505.   /* See if they match */
  506.  
  507.   return (strcmp (execname, corename) ? false : true);
  508.  
  509. #else
  510.  
  511.   return (true);
  512.  
  513. #endif    /* HAVE_PROCFS */
  514. }
  515.  
  516. /* ELF core files contain a segment of type PT_NOTE, that holds much of
  517.    the information that would normally be available from the /proc interface
  518.    for the process, at the time the process dumped core.  Currently this
  519.    includes copies of the prstatus, prpsinfo, and fpregset structures.
  520.  
  521.    Since these structures are potentially machine dependent in size and
  522.    ordering, bfd provides two levels of support for them.  The first level,
  523.    available on all machines since it does not require that the host
  524.    have /proc support or the relevant include files, is to create a bfd
  525.    section for each of the prstatus, prpsinfo, and fpregset structures,
  526.    without any interpretation of their contents.  With just this support,
  527.    the bfd client will have to interpret the structures itself.  Even with
  528.    /proc support, it might want these full structures for it's own reasons.
  529.  
  530.    In the second level of support, where HAVE_PROCFS is defined, bfd will
  531.    pick apart the structures to gather some additional information that
  532.    clients may want, such as the general register set, the name of the
  533.    exec'ed file and its arguments, the signal (if any) that caused the
  534.    core dump, etc.
  535.  
  536.    */
  537.  
  538. static boolean
  539. DEFUN(elf_corefile_note, (abfd, hdr),
  540.       bfd               *abfd AND
  541.       Elf_Internal_Phdr *hdr)
  542. {
  543.   Elf_External_Note *x_note_p;    /* Elf note, external form */
  544.   Elf_Internal_Note i_note;    /* Elf note, internal form */
  545.   char *buf = NULL;        /* Entire note segment contents */
  546.   char *namedata;        /* Name portion of the note */
  547.   char *descdata;        /* Descriptor portion of the note */
  548.   char *sectname;        /* Name to use for new section */
  549.   long filepos;            /* File offset to descriptor data */
  550.   asection *newsect;
  551.  
  552.   if (hdr -> p_filesz > 0
  553.       && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL
  554.       && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L
  555.       && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
  556.     {
  557.       x_note_p = (Elf_External_Note *) buf;
  558.       while ((char *) x_note_p < (buf + hdr -> p_filesz))
  559.     {
  560.       i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz);
  561.       i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz);
  562.       i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type);
  563.       namedata = x_note_p -> name;
  564.       descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
  565.       filepos = hdr -> p_offset + (descdata - buf);
  566.       switch (i_note.type) {
  567.         case NT_PRSTATUS:
  568.           /* process descdata as prstatus info */
  569.           bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
  570.           sectname = ".prstatus";
  571.           break;
  572.         case NT_FPREGSET:
  573.           /* process descdata as fpregset info */
  574.           bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
  575.           sectname = ".fpregset";
  576.           break;
  577.         case NT_PRPSINFO:
  578.           /* process descdata as prpsinfo */
  579.           bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
  580.           sectname = ".prpsinfo";
  581.           break;
  582.         default:
  583.           /* Unknown descriptor, just ignore it. */
  584.           sectname = NULL;
  585.           break;
  586.       }
  587.       if (sectname != NULL)
  588.         {
  589.           newsect = bfd_make_section (abfd, sectname);
  590.           newsect -> _raw_size = i_note.descsz;
  591.           newsect -> filepos = filepos;
  592.           newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  593.           newsect -> alignment_power = 2;
  594.         }
  595.       x_note_p = (Elf_External_Note *)
  596.             (descdata + BFD_ALIGN (i_note.descsz, 4));
  597.     }
  598.     }
  599.   if (buf != NULL)
  600.     {
  601.       free (buf);
  602.     }
  603.   return true;
  604.   
  605. }
  606.  
  607.  
  608. /* Read a specified number of bytes at a specified offset in an ELF
  609.    file, into a newly allocated buffer, and return a pointer to the 
  610.    buffer. */
  611.  
  612. static char *
  613. DEFUN(elf_read, (abfd, offset, size),
  614.       bfd    *abfd AND
  615.       long    offset AND
  616.       int    size)
  617. {
  618.   char *buf;
  619.  
  620.   if ((buf = bfd_alloc (abfd, size)) == NULL)
  621.     {
  622.       bfd_error = no_memory;
  623.       return (NULL);
  624.     }
  625.   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
  626.     {
  627.       bfd_error = system_call_error;
  628.       return (NULL);
  629.     }
  630.   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
  631.     {
  632.       bfd_error = system_call_error;
  633.       return (NULL);
  634.     }
  635.   return (buf);
  636. }
  637.  
  638. /* Begin processing a given object.
  639.  
  640.    First we validate the file by reading in the ELF header and checking
  641.    the magic number.
  642.  
  643.    */
  644.  
  645. static bfd_target *
  646. DEFUN (elf_object_p, (abfd), bfd *abfd)
  647. {
  648.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  649.   Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
  650.   Elf_External_Shdr x_shdr;    /* Section header table entry, external form */
  651.   Elf_Internal_Shdr *i_shdrp;    /* Section header table, internal form */
  652.   unsigned int shindex;
  653.   char *shstrtab;        /* Internal copy of section header stringtab */
  654.   int shstrtabsize;        /* Size of section header string table */
  655.   Elf_Off offset;        /* Temp place to stash file offsets */
  656.   
  657.   /* Read in the ELF header in external format.  */
  658.  
  659.   if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  660.     {
  661.       bfd_error = system_call_error;
  662.       return (NULL);
  663.     }
  664.  
  665.   /* Now check to see if we have a valid ELF file, and one that BFD can
  666.      make use of.  The magic number must match, the address size ('class')
  667.      and byte-swapping must match our XVEC entry, and it must have a
  668.      section header table (FIXME: See comments re sections at top of this
  669.      file). */
  670.  
  671.   if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  672.       x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  673.       x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  674.       x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
  675.     {
  676. wrong:
  677.       bfd_error = wrong_format;
  678.       return (NULL);
  679.     }
  680.  
  681.   /* FIXME, Check EI_VERSION here !  */
  682.  
  683.   switch (x_ehdr.e_ident[EI_CLASS]) {
  684.   case ELFCLASSNONE:            /* address size not specified */
  685.     goto wrong;            /* No support if can't tell address size */
  686.   case ELFCLASS32:            /* 32-bit addresses */
  687.     break;
  688.   case ELFCLASS64:            /* 64-bit addresses */
  689.     goto wrong;            /* FIXME: 64 bits not yet supported */
  690.   default:
  691.     goto wrong;            /* No support if unknown address class */
  692.   }
  693.  
  694.   /* Switch xvec to match the specified byte order.  */
  695.   switch (x_ehdr.e_ident[EI_DATA]) {
  696.   case ELFDATA2MSB:            /* Big-endian */ 
  697.     if (!abfd->xvec->header_byteorder_big_p)
  698.       goto wrong;
  699.     break;
  700.   case ELFDATA2LSB:            /* Little-endian */
  701.     if (abfd->xvec->header_byteorder_big_p)
  702.       goto wrong;
  703.     break;
  704.   case ELFDATANONE:            /* No data encoding specified */
  705.   default:                /* Unknown data encoding specified */
  706.     goto wrong;
  707.   }
  708.   
  709.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  710.      the tdata pointer in the bfd. */
  711.  
  712.   if ((abfd -> tdata.elf_obj_data = 
  713.        (elf_obj_tdata*) bfd_zalloc (abfd, sizeof (elf_obj_tdata))) 
  714.       == NULL)
  715.     {
  716.       bfd_error = no_memory;
  717.       return (NULL);
  718.     }
  719.  
  720.   /* Now that we know the byte order, swap in the rest of the header */
  721.   elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
  722.  
  723.   /* If there is no section header table, we're hosed. */
  724.   if (i_ehdr.e_shoff == 0)
  725.     goto wrong;
  726.  
  727.   if (i_ehdr.e_type == ET_EXEC || i_ehdr.e_type == ET_DYN)
  728.     {
  729.       abfd -> flags |= EXEC_P;
  730.     }
  731.  
  732.   /* Allocate space for a copy of the section header table in
  733.      internal form, seek to the section header table in the file,
  734.      read it in, and convert it to internal form.  As a simple sanity
  735.      check, verify that the what BFD thinks is the size of each section
  736.      header table entry actually matches the size recorded in the file. */
  737.  
  738.   if (i_ehdr.e_shentsize != sizeof (x_shdr))
  739.     goto wrong;
  740.   if ((i_shdrp = (Elf_Internal_Shdr *)
  741.        bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdr.e_shnum)) == NULL)
  742.     {
  743.       bfd_error = no_memory;
  744.       return (NULL);
  745.     }
  746.   if (bfd_seek (abfd, i_ehdr.e_shoff, SEEK_SET) == -1)
  747.     {
  748.       bfd_error = system_call_error;
  749.       return (NULL);
  750.     }
  751.   for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++)
  752.     {
  753.       if (bfd_read ((PTR) &x_shdr, sizeof (x_shdr), 1, abfd)
  754.       != sizeof (x_shdr))
  755.     {
  756.       bfd_error = system_call_error;
  757.       return (NULL);
  758.     }
  759.       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
  760.     }
  761.  
  762.   /* Read in the string table containing the names of the sections.  We
  763.      will need the base pointer to this table later. */
  764.  
  765.   shstrtabsize = i_shdrp[i_ehdr.e_shstrndx].sh_size;
  766.   offset = i_shdrp[i_ehdr.e_shstrndx].sh_offset;
  767.   if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL)
  768.     {
  769.       return (NULL);
  770.     }
  771.  
  772.   /* Once all of the section headers have been read and converted, we
  773.      can start processing them.  Note that the first section header is
  774.      a dummy placeholder entry, so we ignore it.
  775.  
  776.      We also watch for the symbol table section and remember the file
  777.      offset and section size for both the symbol table section and the
  778.      associated string table section. */
  779.  
  780.   for (shindex = 1; shindex < i_ehdr.e_shnum; shindex++)
  781.     {
  782.       Elf_Internal_Shdr *hdr = i_shdrp + shindex;
  783.       bfd_section_from_shdr (abfd, hdr, shstrtab);
  784.       if (hdr -> sh_type == SHT_SYMTAB)
  785.     {
  786.       elf_symtab_filepos (abfd) = hdr -> sh_offset;
  787.       elf_symtab_filesz (abfd) = hdr -> sh_size;
  788.       elf_strtab_filepos (abfd) = (i_shdrp + hdr -> sh_link) -> sh_offset;
  789.       elf_strtab_filesz (abfd) = (i_shdrp + hdr -> sh_link) -> sh_size;
  790.     }
  791.     }
  792.  
  793.   /* Remember the entry point specified in the ELF file header. */
  794.  
  795.   bfd_get_start_address (abfd) = i_ehdr.e_entry;
  796.  
  797.   return (abfd->xvec);
  798. }
  799.  
  800. /*  Core files are simply standard ELF formatted files that partition
  801.     the file using the execution view of the file (program header table)
  802.     rather than the linking view.  In fact, there is no section header
  803.     table in a core file.
  804.  
  805.     The process status information (including the contents of the general
  806.     register set) and the floating point register set are stored in a
  807.     segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
  808.     that allow standard bfd access to the general registers (.reg) and the
  809.     floating point registers (.reg2).
  810.  
  811.  */
  812.  
  813. static bfd_target *
  814. DEFUN (elf_core_file_p, (abfd), bfd *abfd)
  815. {
  816.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  817.   Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
  818.   Elf_External_Phdr x_phdr;    /* Program header table entry, external form */
  819.   Elf_Internal_Phdr *i_phdrp;    /* Program header table, internal form */
  820.   unsigned int phindex;
  821.   
  822.   /* Read in the ELF header in external format.  */
  823.  
  824.   if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  825.     {
  826.       bfd_error = system_call_error;
  827.       return (NULL);
  828.     }
  829.  
  830.   /* Now check to see if we have a valid ELF file, and one that BFD can
  831.      make use of.  The magic number must match, the address size ('class')
  832.      and byte-swapping must match our XVEC entry, and it must have a
  833.      program header table (FIXME: See comments re segments at top of this
  834.      file). */
  835.  
  836.   if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
  837.       x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
  838.       x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
  839.       x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
  840.     {
  841. wrong:
  842.       bfd_error = wrong_format;
  843.       return (NULL);
  844.     }
  845.  
  846.   /* FIXME, Check EI_VERSION here !  */
  847.  
  848.   switch (x_ehdr.e_ident[EI_CLASS]) {
  849.   case ELFCLASSNONE:            /* address size not specified */
  850.     goto wrong;            /* No support if can't tell address size */
  851.   case ELFCLASS32:            /* 32-bit addresses */
  852.     break;
  853.   case ELFCLASS64:            /* 64-bit addresses */
  854.     goto wrong;            /* FIXME: 64 bits not yet supported */
  855.   default:
  856.     goto wrong;            /* No support if unknown address class */
  857.   }
  858.  
  859.   /* Switch xvec to match the specified byte order.  */
  860.   switch (x_ehdr.e_ident[EI_DATA]) {
  861.   case ELFDATA2MSB:            /* Big-endian */ 
  862.     abfd->xvec = &elf_big_vec;
  863.     break;
  864.   case ELFDATA2LSB:            /* Little-endian */
  865.     abfd->xvec = &elf_little_vec;
  866.     break;
  867.   case ELFDATANONE:            /* No data encoding specified */
  868.   default:                /* Unknown data encoding specified */
  869.     goto wrong;
  870.   }
  871.   
  872.   /* Now that we know the byte order, swap in the rest of the header */
  873.   elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
  874.  
  875.   /* If there is no program header, or the type is not a core file, then
  876.      we are hosed. */
  877.   if (i_ehdr.e_phoff == 0 || i_ehdr.e_type != ET_CORE)
  878.     goto wrong;
  879.  
  880.   /* Allocate an instance of the elf_core_tdata structure and hook it up to
  881.      the tdata pointer in the bfd. */
  882.  
  883.   if ((abfd -> tdata.elf_core_data =
  884.        (elf_core_tdata *) bfd_zalloc (abfd, sizeof (elf_core_tdata))) 
  885.       == NULL)
  886.     {
  887.       bfd_error = no_memory;
  888.       return (NULL);
  889.     }
  890.  
  891.   /* Allocate space for a copy of the program header table in
  892.      internal form, seek to the program header table in the file,
  893.      read it in, and convert it to internal form.  As a simple sanity
  894.      check, verify that the what BFD thinks is the size of each program
  895.      header table entry actually matches the size recorded in the file. */
  896.  
  897.   if (i_ehdr.e_phentsize != sizeof (x_phdr))
  898.     goto wrong;
  899.   if ((i_phdrp = (Elf_Internal_Phdr *)
  900.     bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdr.e_phnum)) == NULL)
  901.     {
  902.       bfd_error = no_memory;
  903.       return (NULL);
  904.     }
  905.   if (bfd_seek (abfd, i_ehdr.e_phoff, SEEK_SET) == -1)
  906.     {
  907.       bfd_error = system_call_error;
  908.       return (NULL);
  909.     }
  910.   for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
  911.     {
  912.       if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd)
  913.       != sizeof (x_phdr))
  914.     {
  915.       bfd_error = system_call_error;
  916.       return (NULL);
  917.     }
  918.       elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
  919.     }
  920.  
  921.   /* Once all of the program headers have been read and converted, we
  922.      can start processing them. */
  923.  
  924.   for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
  925.     {
  926.       bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
  927.       if ((i_phdrp + phindex) -> p_type == PT_NOTE)
  928.     {
  929.       elf_corefile_note (abfd, i_phdrp + phindex);
  930.     }
  931.     }
  932.  
  933.   /* Remember the entry point specified in the ELF file header. */
  934.  
  935.   bfd_get_start_address (abfd) = i_ehdr.e_entry;
  936.  
  937.   return (abfd->xvec);
  938. }
  939.  
  940. static boolean
  941. DEFUN (elf_mkobject, (abfd), bfd *abfd)
  942. {
  943.   fprintf (stderr, "elf_mkobject unimplemented\n");
  944.   fflush (stderr);
  945.   abort ();
  946.   return (false);
  947. }
  948.  
  949. static boolean
  950. DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
  951. {
  952.   fprintf (stderr, "elf_write_object_contents unimplemented\n");
  953.   fflush (stderr);
  954.   abort ();
  955.   return (false);
  956. }
  957.  
  958. /* Given an index of a section, retrieve a pointer to it.  Note
  959.    that for our purposes, sections are indexed by {1, 2, ...} with
  960.    0 being an illegal index. */
  961.  
  962. static struct sec *
  963. DEFUN (section_from_bfd_index, (abfd, index),
  964.        bfd            *abfd AND
  965.        int             index)
  966. {
  967.   if (index > 0)
  968.     {
  969.       struct sec *answer = abfd -> sections;
  970.       while (--index > 0)
  971.     {
  972.       answer = answer -> next;
  973.     }
  974.       return (answer);
  975.     }
  976.   return (NULL);
  977. }
  978.  
  979. static boolean
  980. DEFUN (elf_slurp_symbol_table, (abfd), bfd *abfd)
  981. {
  982.   int symcount;        /* Number of external ELF symbols */
  983.   char *strtab;        /* Buffer for raw ELF string table section */
  984.   asymbol *sym;        /* Pointer to current bfd symbol */
  985.   asymbol *symbase;    /* Buffer for generated bfd symbols */
  986.   asymbol **vec;    /* Pointer to current bfd symbol pointer */
  987.   Elf_Internal_Sym i_sym;
  988.   Elf_External_Sym x_sym;
  989.  
  990.   if (bfd_get_outsymbols (abfd) != NULL)
  991.     {
  992.       return (true);
  993.     }
  994.  
  995.   /* Slurp in the string table.  We will keep it around permanently, as
  996.      long as the bfd is in use, since we will end up setting up pointers
  997.      into it for the names of all the symbols. */
  998.  
  999.   strtab = elf_read (abfd, elf_strtab_filepos(abfd), elf_strtab_filesz(abfd));
  1000.   if (strtab == NULL)
  1001.     {
  1002.       return (false);
  1003.     }
  1004.  
  1005.   /* Read each raw ELF symbol, converting from external ELF form to
  1006.      internal ELF form, and then using the information to create a
  1007.      canonical bfd symbol table entry.
  1008.  
  1009.      Note that be allocate the initial bfd canonical symbol buffer
  1010.      based on a one-to-one mapping of the ELF symbols to canonical
  1011.      symbols.  However, it is likely that not all the ELF symbols will
  1012.      be used, so there will be some space leftover at the end.  Once
  1013.      we know how many symbols we actual generate, we realloc the buffer
  1014.      to the correct size and then build the pointer vector. */
  1015.  
  1016.   if (bfd_seek (abfd, elf_symtab_filepos (abfd), SEEK_SET) == -1)
  1017.     {
  1018.       bfd_error = system_call_error;
  1019.       return (false);
  1020.     }
  1021.  
  1022.   symcount = elf_symtab_filesz(abfd) / sizeof (Elf_External_Sym);
  1023.   sym = symbase = (asymbol *) bfd_zalloc (abfd, symcount * sizeof (asymbol));
  1024.  
  1025.   while (symcount-- > 0)
  1026.     {
  1027.       if (bfd_read ((PTR) &x_sym, sizeof (x_sym), 1, abfd) != sizeof (x_sym))
  1028.     {
  1029.       bfd_error = system_call_error;
  1030.       return (false);
  1031.     }
  1032.       elf_swap_symbol_in (abfd, &x_sym, &i_sym);
  1033.       if (i_sym.st_name > 0)
  1034.     {
  1035.       sym -> the_bfd = abfd;
  1036.       sym -> name = strtab + i_sym.st_name;
  1037.       sym -> value = i_sym.st_value;
  1038.       if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
  1039.         {
  1040.           /* Note:  This code depends upon there being an ordered
  1041.          one-for-one mapping of ELF sections to bfd sections. */
  1042.           sym -> section = section_from_bfd_index (abfd, i_sym.st_shndx);
  1043.         }
  1044.       else if (i_sym.st_shndx == SHN_ABS)
  1045.         {
  1046.           sym -> section = &bfd_abs_section;
  1047.         }
  1048.       else if (i_sym.st_shndx == SHN_COMMON)
  1049.         {
  1050.           sym -> section = &bfd_com_section;
  1051.         }
  1052.       switch (ELF_ST_BIND (i_sym.st_info))
  1053.         {
  1054.           case STB_LOCAL:
  1055.         sym -> flags |= BSF_LOCAL;
  1056.             break;
  1057.           case STB_GLOBAL:
  1058.             sym -> flags |= (BSF_GLOBAL | BSF_EXPORT);
  1059.             break;
  1060.           case STB_WEAK:
  1061.         sym -> flags |= BSF_WEAK;
  1062.             break;
  1063.         }
  1064.       sym++;
  1065.     }
  1066.     }
  1067.  
  1068.   bfd_get_symcount(abfd) = symcount = sym - symbase;
  1069.   sym = symbase = (asymbol *)
  1070.     bfd_realloc (abfd, symbase, symcount * sizeof (asymbol));
  1071.   bfd_get_outsymbols(abfd) = vec = (asymbol **)
  1072.     bfd_alloc (abfd, symcount * sizeof (asymbol *));
  1073.  
  1074.   while (symcount-- > 0)
  1075.     {
  1076.       *vec++ = sym++;
  1077.     }
  1078.  
  1079.   return (true);
  1080. }
  1081.  
  1082. /* Return the number of bytes required to hold the symtab vector.
  1083.  
  1084.    Note that we base it on the count plus 1, since we will null terminate
  1085.    the vector allocated based on this size. */
  1086.  
  1087. static unsigned int
  1088. DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd)
  1089. {
  1090.   unsigned int symtab_size = 0;
  1091.  
  1092.   if (elf_slurp_symbol_table (abfd))
  1093.     {
  1094.       symtab_size = (bfd_get_symcount (abfd) + 1) * (sizeof (asymbol));
  1095.     }
  1096.   return (symtab_size);
  1097. }
  1098.  
  1099. static unsigned int
  1100. elf_get_reloc_upper_bound (abfd, asect)
  1101. bfd            *abfd;
  1102. sec_ptr         asect;
  1103. {
  1104.   fprintf (stderr, "elf_get_reloc_upper_bound unimplemented\n");
  1105.   fflush (stderr);
  1106.   abort ();
  1107.   return (0);
  1108. }
  1109.  
  1110. static unsigned int
  1111. elf_canonicalize_reloc (abfd, section, relptr, symbols)
  1112. bfd            *abfd;
  1113. sec_ptr         section;
  1114. arelent       **relptr;
  1115. asymbol       **symbols;
  1116. {
  1117.   fprintf (stderr, "elf_canonicalize_reloc unimplemented\n");
  1118.   fflush (stderr);
  1119.   abort ();
  1120.   return (0);
  1121. }
  1122.  
  1123. static unsigned int
  1124. DEFUN (elf_get_symtab, (abfd, alocation),
  1125.        bfd            *abfd AND
  1126.        asymbol       **alocation)
  1127. {
  1128.   unsigned int symcount;
  1129.   asymbol **vec;
  1130.  
  1131.   if (!elf_slurp_symbol_table (abfd))
  1132.     {
  1133.       return (0);
  1134.     }
  1135.   else
  1136.     {
  1137.       symcount = bfd_get_symcount (abfd);
  1138.       vec = bfd_get_outsymbols (abfd);
  1139.       while (symcount-- > 0)
  1140.     {
  1141.       *alocation++ = *vec++;
  1142.     }
  1143.       *alocation++ = NULL;
  1144.       return (bfd_get_symcount (abfd));
  1145.     }
  1146. }
  1147.  
  1148. static asymbol *
  1149. DEFUN (elf_make_empty_symbol, (abfd),
  1150.        bfd *abfd)
  1151. {
  1152.   elf_symbol_type *new;
  1153.  
  1154.   new = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
  1155.   if (new == NULL)
  1156.     {
  1157.       bfd_error = no_memory;
  1158.       return (NULL);
  1159.     }
  1160.   else
  1161.     {
  1162.       new -> symbol.the_bfd = abfd;
  1163.       return (&new -> symbol);
  1164.     }
  1165. }
  1166.  
  1167. static void 
  1168. DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
  1169.       bfd            *ignore_abfd AND
  1170.       PTR           filep AND
  1171.       asymbol        *symbol AND
  1172.       bfd_print_symbol_type how)
  1173. {
  1174.   fprintf (stderr, "elf_print_symbol unimplemented\n");
  1175.   fflush (stderr);
  1176.   abort ();
  1177. }
  1178.  
  1179. static alent *
  1180. DEFUN (elf_get_lineno,(ignore_abfd, symbol),
  1181.       bfd            *ignore_abfd AND
  1182.       asymbol        *symbol)
  1183. {
  1184.   fprintf (stderr, "elf_get_lineno unimplemented\n");
  1185.   fflush (stderr);
  1186.   abort ();
  1187.   return (NULL);
  1188. }
  1189.  
  1190. static boolean
  1191. DEFUN (elf_set_arch_mach,(abfd, arch, machine),
  1192.       bfd            *abfd AND
  1193.       enum bfd_architecture arch AND
  1194.       unsigned long   machine)
  1195. {
  1196.   fprintf (stderr, "elf_set_arch_mach unimplemented\n");
  1197.   fflush (stderr);
  1198.   /* Allow any architecture to be supported by the elf backend */
  1199.   return  bfd_default_set_arch_mach(abfd, arch, machine);
  1200. }
  1201.  
  1202. static boolean
  1203. DEFUN (elf_find_nearest_line,(abfd,
  1204.                   section,
  1205.                   symbols,
  1206.                   offset,
  1207.                   filename_ptr,
  1208.                   functionname_ptr,
  1209.                   line_ptr),
  1210.       bfd            *abfd AND
  1211.       asection       *section AND
  1212.       asymbol       **symbols AND
  1213.       bfd_vma         offset AND
  1214.       CONST char      **filename_ptr AND
  1215.       CONST char       **functionname_ptr AND
  1216.       unsigned int   *line_ptr)
  1217. {
  1218.   fprintf (stderr, "elf_find_nearest_line unimplemented\n");
  1219.   fflush (stderr);
  1220.   abort ();
  1221.   return (false);
  1222. }
  1223.  
  1224. static int 
  1225. DEFUN (elf_sizeof_headers, (abfd, reloc),
  1226.       bfd *abfd AND
  1227.       boolean reloc)
  1228. {
  1229.   fprintf (stderr, "elf_sizeof_headers unimplemented\n");
  1230.   fflush (stderr);
  1231.   abort ();
  1232.   return (0);
  1233. }
  1234.  
  1235. /* This structure contains everything that BFD knows about a target.
  1236.    It includes things like its byte order, name, what routines to call
  1237.    to do various operations, etc.  Every BFD points to a target structure
  1238.    with its "xvec" member.
  1239.  
  1240.    There are two such structures here:  one for big-endian machines and
  1241.    one for little-endian machines.   */
  1242.  
  1243. /* Archives are generic or unimplemented.  */
  1244. #define elf_slurp_armap            bfd_false
  1245. #define elf_slurp_extended_name_table    _bfd_slurp_extended_name_table
  1246. #define elf_truncate_arname        bfd_dont_truncate_arname
  1247. #define elf_openr_next_archived_file    bfd_generic_openr_next_archived_file
  1248. #define elf_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  1249. #define    elf_write_armap            (PROTO (boolean, (*),        \
  1250.      (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count,    \
  1251.       int stridx))) bfd_false
  1252.  
  1253. /* Ordinary section reading and writing */
  1254. #define elf_new_section_hook        _bfd_dummy_new_section_hook
  1255. #define    elf_get_section_contents    bfd_generic_get_section_contents
  1256. #define    elf_set_section_contents    bfd_generic_set_section_contents
  1257. #define    elf_close_and_cleanup        bfd_generic_close_and_cleanup
  1258.  
  1259. #define elf_bfd_debug_info_start    bfd_void
  1260. #define elf_bfd_debug_info_end        bfd_void
  1261. #define elf_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
  1262. #define elf_bfd_get_relocated_section_contents \
  1263.  bfd_generic_get_relocated_section_contents
  1264. #define elf_bfd_relax_section bfd_generic_relax_section
  1265. bfd_target elf_big_vec =
  1266. {
  1267.   /* name: identify kind of target */
  1268.   "elf-big",
  1269.  
  1270.   /* flavour: general indication about file */
  1271.   bfd_target_elf_flavour,
  1272.  
  1273.   /* byteorder_big_p: data is big endian */
  1274.   true,
  1275.  
  1276.   /* header_byteorder_big_p: header is also big endian */
  1277.   true,
  1278.  
  1279.   /* object_flags: mask of all file flags */
  1280.   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
  1281.    DYNAMIC | WP_TEXT),
  1282.   
  1283.   /* section_flags: mask of all section flags */
  1284.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
  1285.    SEC_DATA), 
  1286.  
  1287.   /* ar_pad_char: pad character for filenames within an archive header
  1288.      FIXME:  this really has nothing to do with ELF, this is a characteristic
  1289.      of the archiver and/or os and should be independently tunable */
  1290.   '/',
  1291.  
  1292.   /* ar_max_namelen: maximum number of characters in an archive header
  1293.      FIXME:  this really has nothing to do with ELF, this is a characteristic
  1294.      of the archiver and should be independently tunable.  This value is
  1295.      a WAG (wild a** guess) */
  1296.   15,
  1297.  
  1298.   /* align_power_min: minimum alignment restriction for any section
  1299.      FIXME:  this value may be target machine dependent */
  1300.   3,
  1301.  
  1302.   /* Routines to byte-swap various sized integers from the data sections */
  1303.   _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
  1304.  
  1305.   /* Routines to byte-swap various sized integers from the file headers */
  1306.   _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
  1307.  
  1308.   /* bfd_check_format: check the format of a file being read */
  1309.   { _bfd_dummy_target,        /* unknown format */
  1310.     elf_object_p,        /* assembler/linker output (object file) */
  1311.     bfd_generic_archive_p,    /* an archive */
  1312.     elf_core_file_p        /* a core file */
  1313.   },
  1314.  
  1315.   /* bfd_set_format: set the format of a file being written */
  1316.   { bfd_false,
  1317.     elf_mkobject,
  1318.     _bfd_generic_mkarchive,
  1319.     bfd_false
  1320.   },
  1321.  
  1322.   /* bfd_write_contents: write cached information into a file being written */
  1323.   { bfd_false,
  1324.     elf_write_object_contents,
  1325.     _bfd_write_archive_contents,
  1326.     bfd_false
  1327.   },
  1328.  
  1329.   /* Initialize a jump table with the standard macro.  All names start
  1330.      with "elf" */
  1331.   JUMP_TABLE(elf),
  1332.  
  1333.   /* SWAP_TABLE */
  1334.   NULL, NULL, NULL
  1335. };
  1336.  
  1337. bfd_target elf_little_vec =
  1338. {
  1339.   /* name: identify kind of target */
  1340.   "elf-little",
  1341.  
  1342.   /* flavour: general indication about file */
  1343.   bfd_target_elf_flavour,
  1344.  
  1345.   /* byteorder_big_p: data is big endian */
  1346.   false,        /* Nope -- this one's little endian */
  1347.  
  1348.   /* header_byteorder_big_p: header is also big endian */
  1349.   false,        /* Nope -- this one's little endian */
  1350.  
  1351.   /* object_flags: mask of all file flags */
  1352.   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
  1353.    DYNAMIC | WP_TEXT),
  1354.   
  1355.   /* section_flags: mask of all section flags */
  1356.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
  1357.    SEC_DATA), 
  1358.  
  1359.   /* ar_pad_char: pad character for filenames within an archive header
  1360.      FIXME:  this really has nothing to do with ELF, this is a characteristic
  1361.      of the archiver and/or os and should be independently tunable */
  1362.   '/',
  1363.  
  1364.   /* ar_max_namelen: maximum number of characters in an archive header
  1365.      FIXME:  this really has nothing to do with ELF, this is a characteristic
  1366.      of the archiver and should be independently tunable.  This value is
  1367.      a WAG (wild a** guess) */
  1368.   15,
  1369.  
  1370.   /* align_power_min: minimum alignment restriction for any section
  1371.      FIXME:  this value may be target machine dependent */
  1372.   3,
  1373.  
  1374.   /* Routines to byte-swap various sized integers from the data sections */
  1375.   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
  1376.  
  1377.   /* Routines to byte-swap various sized integers from the file headers */
  1378.   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
  1379.  
  1380.   /* bfd_check_format: check the format of a file being read */
  1381.   { _bfd_dummy_target,        /* unknown format */
  1382.     elf_object_p,        /* assembler/linker output (object file) */
  1383.     bfd_generic_archive_p,    /* an archive */
  1384.     elf_core_file_p        /* a core file */
  1385.   },
  1386.  
  1387.   /* bfd_set_format: set the format of a file being written */
  1388.   { bfd_false,
  1389.     elf_mkobject,
  1390.     _bfd_generic_mkarchive,
  1391.     bfd_false
  1392.   },
  1393.  
  1394.   /* bfd_write_contents: write cached information into a file being written */
  1395.   { bfd_false,
  1396.     elf_write_object_contents,
  1397.     _bfd_write_archive_contents,
  1398.     bfd_false
  1399.   },
  1400.  
  1401.   /* Initialize a jump table with the standard macro.  All names start
  1402.      with "elf" */
  1403.   JUMP_TABLE(elf),
  1404.  
  1405.   /* SWAP_TABLE */
  1406.   NULL, NULL, NULL
  1407. };
  1408.